package cn.edu.cqvie.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;

public class ByteBufTest1 {

    public static void main(String[] args) {
        ByteBuf buf = Unpooled.copiedBuffer("张三 hello world!", Charset.forName("utf-8"));

        if (buf.hasArray()) { //是否是堆上的缓冲
            byte[] content = buf.array();
            System.out.println(new String(content, Charset.forName("utf-8")));

            System.out.println(buf);

            System.out.println(buf.toString(Charset.forName("utf-8")));

            //数组的偏移量，基于数组的偏移量
            System.out.println(buf.arrayOffset());
            System.out.println(buf.readerIndex());
            System.out.println(buf.writerIndex());
            System.out.println(buf.capacity());

            int len = buf.readableBytes();
            System.out.println(len);


            for (int i =0 ; i < buf.readableBytes(); ++i) {
                System.out.println((char) buf.getByte(i));
            }

            System.out.println(buf.getCharSequence(0, 6, Charset.forName("utf-8")));
        }
    }
}
